//
//  MXUserTableView.m
//  Stratups
//
//  Created by 王启颖 on 2019/2/27.
//  Copyright © 2019 王启颖. All rights reserved.
//

#import "MXUserTableView.h"
#import "MXHomePageCell.h"
#import "MXHomePageCell.h"
#import "MXHomePageModel.h"


@interface MXUserTableView () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (strong, nonatomic) NSMutableArray *rowHeightArr;

@property (nonatomic, assign) int currentPage;

@property (strong, nonatomic) NSMutableDictionary *dicParams;


@end
@implementation MXUserTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.bounds];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = MX_WHITE_COLOR;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        //        self.tableView.tableHeaderView = self.headView;
        
        [self addSubview:self.tableView];
        [self addHeaderRefresh];
        
        
        
        
    }
    return self;
}


#pragma mark - 上下拉刷新事件 -
- (void)addHeaderRefresh{
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

-(void)refresh{
    self.currentPage = 1;
    
    [self.paramsArr removeAllObjects];
    [self.rowHeightArr removeAllObjects];
    [self getMessagesRequest];
}

-(void)loadMore{
    self.currentPage ++;
    [self getMessagesRequest];
}

#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_paramsArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MXHomePageCell *cell = [MXHomePageCell cellWithTableView:tableView];
    cell.navigationController = self.navigationController;
    MXHomePageModel *model = _paramsArr[indexPath.row];
    cell.model = model;
    
    __weak typeof(self) weakself = self;
    cell.agreesBlock = ^(MXHomePageModel *model) {
        weakself.paramsArr[indexPath.row] = model;
    };
    
    cell.commentsBlock = ^(MXHomePageModel *model) {
        
    };
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *row_height_number = self.rowHeightArr[indexPath.row];
    return [row_height_number floatValue];
}


#pragma mark - 懒加载 -
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

-(NSMutableArray *)rowHeightArr{
    if (!_rowHeightArr) {
        _rowHeightArr = [[NSMutableArray alloc]init];
    }
    return _rowHeightArr;
}

- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    [self initdicParams];
    return _dicParams;
}
- (void)initdicParams {
    _dicParams[@"pageNum"] = [@(self.currentPage) description];
    _dicParams[@"pageSize"] = @"5";
    _dicParams[@"challenger_id"] = self.user_id;
    _dicParams[@"user_id"] = [MXUserMessageTool message].user_id;
}

- (void)setUser_id:(NSString *)user_id{
    _user_id = user_id;
    
    [self refresh];
}

#pragma mark - 网络请求 -

-(void)getMessagesRequest{
    [SVProgressHUD show];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:getChallengerMessages params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (code == 0) {
            
            NSLog(@"%@",result);
            
            NSArray *messArr = result[@"message"];
            if ([messArr count] == 0) {
                self.tableView.mj_footer = nil;
            }else{
                self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
            }
            
            for (id res in result[@"message"]) {
                MXHomePageModel *model = [MXHomePageModel mj_objectWithKeyValues:res];
                [self.paramsArr addObject:model];
                
                [self setupRowHeightArr:model];
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 其他 -
- (void)setupRowHeightArr:(MXHomePageModel *)model{
    CGFloat lab_height = [model.messages_info caculateTextHeightWithSize:13];
    
    CGFloat pic_height = 0;
    if (model.picturesList.count > 0) {
        Picture *pic = [Picture mj_objectWithKeyValues:model.picturesList[0]];
        pic_height = [pic.picture_size caculateToPicHeight];
    }
    
    //93是头像栏+文字的上下高，44是点赞栏，20是点赞和评论，40是标签栏
    CGFloat row_height = lab_height + pic_height + 93 + 44 + 20;
    NSNumber *row_height_number = [NSNumber numberWithFloat:row_height];
    [self.rowHeightArr addObject:row_height_number];
}


@end
